/*
 * Professional Android, 4th Edition
 * Reto Meier and Ian Lake
 * Copyright 2018 John Wiley Wiley & Sons, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.professionalandroid.apps.earthquake;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;


import com.professionalandroid.apps.earthquake.databinding.ListItemEarthquakeBinding;
import java.text.MessageFormat;

public class EarthquakeRecyclerViewAdapter extends
  RecyclerView.Adapter<EarthquakeRecyclerViewAdapter.ViewHolder> {

  private final List<Earthquake> mEarthquakes;
  private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
  private static final NumberFormat MAGNITUDE_FORMAT =new DecimalFormat("0.0");
   private   static final MessageFormat wxz_FORMAT =new MessageFormat( "wxz") ;/*ToDo 晚点查一下MessageFormat用法*/
  public EarthquakeRecyclerViewAdapter(List<Earthquake> earthquakes ) {
    mEarthquakes = earthquakes;
  }

  @Override
  public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_earthquake,    parent, false);
//    //初始化ListItemEarthquakeBinding 的 方法一
//    ListItemEarthquakeBinding binding =DataBindingUtil.bind(view);
//    return new ViewHolder(view);

//    //初始化ListItemEarthquakeBinding 的 方法二
//    ListItemEarthquakeBinding binding = DataBindingUtil.setContentView(this,R.layout.list_item_earthquake);
    //初始化ListItemEarthquakeBinding 的 方法三
    ListItemEarthquakeBinding binding = ListItemEarthquakeBinding.inflate( LayoutInflater.from(parent.getContext()), parent, false);
    return new ViewHolder(binding);
  }

  @Override
  public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
//    holder.earthquake = mEarthquakes.get(position);
//    holder.detailsTextView.setText(mEarthquakes.get(position).toString());
    Earthquake earthquake = mEarthquakes.get(position);
    holder.binding.setEarthquake(earthquake);
    holder.binding.listItemEarthquakeDetails.setText(mEarthquakes.get(position).toString());
    holder.binding.executePendingBindings();//避免滚动时闪烁
  }

  @Override
  public int getItemCount() {
    return mEarthquakes.size();
  }














  public class ViewHolder extends RecyclerView.ViewHolder {
//    public final View parentView;
//    public final TextView detailsTextView;
//    public Earthquake earthquake;
    public final ListItemEarthquakeBinding binding;

//    public ViewHolder(View view) {
//      super(view);
//      parentView = view;
//      detailsTextView = (TextView)view.findViewById(R.id.list_item_earthquake_details);
//    }
    public ViewHolder(ListItemEarthquakeBinding binding) {
      super(binding.getRoot());
      this.binding = binding;
      binding.setTimeformat(TIME_FORMAT);
      binding.setMagnitudeformat(MAGNITUDE_FORMAT);
      binding.setStringWxzFormat(wxz_FORMAT);
    }

//    @Override
//    public String toString() {
//      return super.toString() + " '" + detailsTextView.getText() + "'";
//    }
  }
}
